package task.manager;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.LinearLayout;
import android.widget.Button;
import android.widget.TextView;

public class CategoryViewActivity extends Activity {

	private CategoryFacade categoryFacade;
	private TaskFacade taskFacade;
	private String[] categoryNames;
	private Button b;
	private Button log0;
	private Button sort;
	private Button map;
	private int state;
	private int mYear;
	private int mMonth;
	private int mDay;
	
	
	
	static final int DATE_DIALOG_ID = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.categoryviewactivity);
		
		final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);

		categoryFacade = CategoryFacade.getInstance();
		taskFacade = TaskFacade.getInstance();
		categoryNames = categoryFacade.getCategoryList();

		b = (Button) findViewById(R.id.addCat);
		sort = (Button)findViewById(R.id.button1);
		log0 = (Button)findViewById(R.id.logoutBtn);
		map = (Button)findViewById(R.id.mapBtn);

		final ListView lv = (ListView) findViewById(R.id.list_view1);
		lv.setTextFilterEnabled(true);

		lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
				categoryNames));

		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				//gets the label of the clicked item
				String selectedCat = (String) lv.getItemAtPosition(position);
				System.out.println(selectedCat);
				
				if(categoryFacade.getSortState() == 2){
					categoryFacade.setActiveCategory(selectedCat);
					taskFacade.setTaskList(categoryFacade.getTasksFromCategory(selectedCat));
					System.out.println("meow");
				
				
					Intent taskScreen = new Intent(getApplicationContext(),
							TaskViewActivity.class);
					System.out.println("Intent finished");
					startActivity(taskScreen);
					System.out.println("startActivity finished");
				}
				else{
					taskFacade.setActiveTask(selectedCat);
					taskFacade.setTaskList(categoryFacade.getTasksFromCategory(categoryFacade.getCategoryOfTask(selectedCat).getName()));
					//System.out.println("meow");
				
				
					Intent taskScreen = new Intent(getApplicationContext(),
							ViewTaskScreen.class);
					System.out.println("Intent finished");
					startActivity(taskScreen);
					System.out.println("startActivity finished");
				}
				
				finish();
				

				// When clicked, show a go to the category's tasks

			}
		});

	
		b.setOnClickListener(new OnClickListener() {
			/**
			 * Response to clicking Add new category.
			 */
			public void onClick(View v) {
				// goto category creation screen
				
				//Intent nextScreen = new Intent(getApplicationContext(),
				//		NewCategory.class);
				//startActivity(nextScreen);
				
				Intent nextScreen = new Intent(getApplicationContext(),
						AddTaskScreen.class);
				startActivity(nextScreen);
				
				
				finish();
			} // End of onClick method.
		}); // End of setOnClickListener.
		
		log0.setOnClickListener(new OnClickListener() {
        	/**
        	 * Response to clicking "Logout".
        	 */
        	public void onClick(View v) {
		  		// goto category creation screen
        		finish();
        	} // End of onClick method.
        }); // End of setOnClickListener.
		
		sort.setOnClickListener(new OnClickListener() {
        	/**
        	 * Response to clicking "Sort".
        	 */
        	@SuppressWarnings("deprecation")
			public void onClick(View v) {
        		showDialog(1);
        	} // End of onClick method.
        }); // End of setOnClickListener.
		
		map.setOnClickListener(new OnClickListener() {
        	/**
        	 * Response to clicking "Map".
        	 */
			public void onClick(View v) {
				Intent mapScreen = new Intent(getApplicationContext(),
						GoogleMapsView.class);
				startActivity(mapScreen);
        	} // End of onClick method.
        }); // End of setOnClickListener.


	}
	
	private DatePickerDialog.OnDateSetListener mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {

                public void onDateSet(DatePicker view, int year, 
                                      int monthOfYear, int dayOfMonth) {
                    mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;
                    
                    categoryFacade.setSortDate(mMonth,mDay,mYear);

                }
            };
            
	
	protected Dialog onCreateDialog(int id) {
		switch (id){
		case DATE_DIALOG_ID:
	        return new DatePickerDialog(this,
	                    mDateSetListener,
	                    mYear, mMonth, mDay);
		
		case 1:
        String[] items = {"All", "Completed Only", "By Category", "By Date"};
		return new AlertDialog.Builder(CategoryViewActivity.this)
        .setIconAttribute(android.R.attr.alertDialogIcon)
        .setTitle("Select Sort Method")
        .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
            	state = whichButton;
            	switch (whichButton){
            	case(3):                
            		showDialog(DATE_DIALOG_ID);
            	}

                /* User clicked on a radio button do some stuff */
            }
        })
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
            	categoryFacade.setState(state);
            	Intent nextScreen = new Intent(getApplicationContext(), LoadScreen.class);
        		startActivity(nextScreen);
        		finish();
                /* User clicked Yes so do some stuff */
            }
        })
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

                /* User clicked No so do some stuff */
            }
        })
       .create();
	}
		return null;
		
	}

}
